<template>
	<view>
		<uni-search-bar :value="value" class="search" placeholder="输入关键词进行搜索" cancel-text="cancel" @confirm="handleConfirm">
			<uni-icons slot="searchIcon" color="#999999" size="18" type="home" />
		</uni-search-bar>
		<view class="">
			<text>历史搜索</text>
			<view class="">
				<uni-tag :text="item" type="primary" v-for="(item, index) in his" :key="index" @click="handleSearch(item)" />
			</view>
		</view>
		<view v-for="(item,index) in list" :key="item.id" class="item">
			<view class="imageWrap">
				<image :src="item.image" mode="" class="image"></image>
			</view>
			<view class="itemDetail">
				<view class="title">{{item.title}}</view>
				<view class="price">¥ {{item.price}}</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				list: [],
				his: uni.getStorageSync('his') || [],
				value: ''
			}
		},
		methods: {
			handleConfirm(e) {
				this.fetchSearch(e.value)
			},
			handleSearch(item) {
				this.value = item
				this.fetchSearch(item)
			},
			fetchSearch(value) {
				uni.request({
					url: 'http://localhost:3000/list',
					data: {
						title_like: value
					}
				}).then(res => {
					const index = this.his.indexOf(value)
					if(index > -1) {
						this.his = this.his.filter(item => item !== value)
					}
					this.his.unshift(value)
					uni.setStorageSync('his', this.his)
					this.list = res.data
				})
			}
		}
	}
</script>

<style lang="scss">
	.item {
		display: flex;
		height: 200rpx;
		margin: 20rpx auto 20rpx;
		width: 702rpx;
		flex-direction: row;
		.imageWrap {
			width: 200rpx;
			height: 200rpx;
			border-radius: 20rpx;
			overflow: hidden;
			flex-shrink: 0;
			.image {
				width: 200rpx;
				height: 200rpx;
			}
		}
		.itemDetail {
			margin-left: 20rpx;
			.title {
				font-size: 34rpx;
			}
			.price {
				font-size: 28rpx;
				color: $uni-color-success;
			}
		}
	}
</style>
